#encoding:utf-8
from wsgiref.simple_server import make_server
import re
from api import api_db,api_test

urls=(
    ("/api","api_test"),
    ("/api/db","api_db"),
)

#函数01
def Application(environ, start_response):
    path = environ['PATH_INFO']
    for url,app in urls:
        #match = re.search(regex, path)
        # print path,"\n",url
        if path ==url:
            app=globals()[app]
            return app(environ, start_response)

    start_response("404 NOT FOUND", [('Content-type', 'text/plain')])
    return ["404 ,page dose not exists"]

#基类02
# class Application(object):
#     def _match(self,path):
#         # path = path.split('/')[1]
#         for url,app in urls:
#             if path == url:
#                 # print path
#                 return app
#
#     def __call__(self,environ,start_response):
#         path = environ['PATH_INFO']
#         app=self._match(path)
#         if app:
#             app = globals()[app]
#             return app(environ, start_response)
#         else:
#             start_response("404 NOT FOUND", [('Content-type', 'text/plain')])
#             return ["404 ,page dose not exists"]


#函数01
httpd = make_server('', 8000,Application)

#基类02
# httpd = make_server('', 8000,Application())
print "Serving HTTP on port 8000..."
httpd.serve_forever()